package cn.ihuizi.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ihuizi.entity.Admin;
import cn.ihuizi.service.AdminService;
import cn.ihuizi.service.impl.AdminServiceImpl;
import cn.ihuizi.util.Admin_Id;

/**
 * Servlet implementation class AdminModiPwd
 */
@WebServlet("/AdminModiPwd")
public class AdminModiPwd extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*response.getWriter().append("Served at: ").append(request.getContextPath());*/
		response.setContentType("text/html;charset=utf-8");
		System.out.println("111");
		String pwd = request.getParameter("pwd");  //页面输入的旧密码
		String pwdold= Admin_Id.pwd;				//数据库里的旧密码
		String pwdnew = request.getParameter("pwdnew");
		String repwdnew = request.getParameter("repwdnew");
		
		System.out.println(pwd);
		String account = Admin_Id.account;
		AdminService adminService = new AdminServiceImpl();
	
		if(pwdnew.equals(repwdnew)) {
			if(pwd.equals(pwdold)) {
				boolean b = adminService.updateAdminPwd(new Admin(account,pwdnew));
				if(b) {
					System.out.println("修改密码成功");
					response.getWriter().write("修改成功！");
				}else {
					response.getWriter().write("修改失败！");
					System.out.println("修改密码失败");
				}
			}else {
				response.getWriter().write("旧密码输入错误，请重新输入！");
			}
		}else {
			response.getWriter().write("两次密码不一致，请重新修改！");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
